package cn.new7as.cas5s.anhjkfu;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.lxj.xpopup.util.XPopupUtils;

import cn.new7as.cas5s.R;
import cn.new7as.cas5s.ui.easy.netapi.PrepareDataApi;

public class PreparePop extends BottomPopupView {

    PrepareDataApi.PreDataData preDataData;
    private Context context;

    public PreparePop(@NonNull Context context, PrepareDataApi.PreDataData data) {
        super(context);
        preDataData = data;
        this.context = context;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.pop_prepare;
    }

    OnConfirmListener xPopupCallback;

    public BasePopupView setPopupCallback(OnConfirmListener xPopupCallback) {
        this.xPopupCallback = xPopupCallback;

        return this;
    }

    int sele;
    TextView amout;
    TextView tenure;
    TextView interest;
    TextView processingfees;

    void shows() {
        amout.setText(preDataData.productInfoVOS.get(sele).loanAmount);
        tenure.setText(preDataData.productInfoVOS.get(sele).periodLength + " Days");
        interest.setText(preDataData.productInfoVOS.get(sele).interest);
        processingfees.setText(preDataData.productInfoVOS.get(sele).serviceCharge);
    }


    void check(int type, TextView textView) {
        final String[] s = {""};
        String[] strings = new String[preDataData.productInfoVOS.size()];
        textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (type == 1) s[0] = "Amount/₹";
                else if (type == 2) s[0] = "Tenure";
                else if (type == 3) s[0] = "Interest";
                else if (type == 4) s[0] = "Processing Fees";
                for (int i = 0; i < strings.length; i++) {
                    if (type == 1) strings[i] = preDataData.productInfoVOS.get(i).loanAmount;
                    else if (type == 2)
                        strings[i] = preDataData.productInfoVOS.get(i).periodLength + " Days";
                    else if (type == 3) strings[i] = preDataData.productInfoVOS.get(i).interest;
                    else if (type == 4)
                        strings[i] = preDataData.productInfoVOS.get(i).serviceCharge;
                }
                new XPopup.Builder(context)
                        .isDestroyOnDismiss(true)

                        .asBottomList(s[0], strings,
                                null, -1,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        sele = position;
                                        shows();
                                    }
                                })
                        .show();
            }
        });

    }

    @Override
    protected void onCreate() {
        super.onCreate();
        ImageView closepop = findViewById(R.id.closepop);
        TextView verifyme = findViewById(R.id.verifyme);
        amout = findViewById(R.id.amounts);
        tenure = findViewById(R.id.tenure);
        interest = findViewById(R.id.interest);
        processingfees = findViewById(R.id.processingfees);
        shows();

        check(1, amout);
        check(2, tenure);
        check(3, interest);
        check(4, processingfees);
        closepop.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();

            }
        });
        verifyme.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                xPopupCallback.nextloan(preDataData.productInfoVOS.get(sele).productCode);
            }
        });
    }


    @Override
    protected void onShow() {
        super.onShow();

    }


    @Override
    protected void onDismiss() {
        Log.e("tag", " onDismiss");
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .7f);
    }
}